Och, it's now time to start getting into the nitty gritty of coordinate frames.
Now see, frames can be quite complex, so I'll try my best to explain them to you here and also leave
some extra documentation by roadblocks on key frames.
In case you didn't fully understand everything or you'd like to see more examples.
The C frame contains two vector three objects.
One represents the position and one represents the rotation.
The position holds the global X, Y and Z coordinates, while the other represents the rotation data
for each of the axes.
The see frame object is also full of a bunch of neat functions that allow us to position parts relative
to another part.
Position parts between two other parts and rotate relative to another part.
We can create a new C frame using the C frame new constructor, and we can either pass a vector three
or three numbers to create and return a C frame that has no rotation.
The constructor for See Frame, as you can notice, is overloaded, meaning that different parameters
will result in different constructors being run.
As another example, if you instead pass to vector threes, then this would return a C frame that has
a position at this first vector three and a rotation that is facing towards the second vector three.
There's also a function in C frame called look at, which is the same as this function here, but allows
us to define one more vector three.
And this vector three represents the up vector of the C frame.
Or basically what direction is up to that C frame By default that is the vector 010, meaning that up
is one unit towards the sky in the Y direction.
There's another function called See Framed on angles, and this returns a C frame with a position of
000.
But it has a rotation that is based in radians.
And we'll get to see this later.
This last function I want to mention here is called lookup and that stands for linear interpolation.
And that's kind of like another math function thing.
But the basic idea of it is that you can create this imaginary line between two points and based on
this line you can position your C frame to be on whatever point on the line.
So the second number here represents a floating point number, and you can make it either zero all the
way up to one.
And that floating point number represents the percentage of distance away from this C frame is to the
C frame.
So if I put like 0.7, that would represent 70% of distance away from this C frame to the C frame.
Okay, let's get started.
Messing around with keyframes.
So I have a variable here I created.
It's just called demo and a reference.
This folder I made in the workspace called C Frame Demo.
And here, here's my little C frame demo.
I have two red parts.
One is called red one, the other is called red.
Two, I have this blue part with a decal on it.
And this decal is facing in the forward facing direction for this part.
So if I right click my part and press this button that says Show orientation indicator, I can see which
direction this part is facing.
This is the front face for the part.
Now, what I'm going to do here is I'm going to have this part become rotated and face towards this
first read part here so I can go into my script.
And I can create a new frame and I'll store it in a variable.
Just call it looking see frame.
And I'll set it to a new frame using the dot new constructor.
And I'll first pass the position of my blue part.
I'm just indexing my reference to the folder, getting blue and getting the position property.
Whoops.
And then the second parameter here, I'm going to set it to be the position of my read part.
So what this is saying is I'm returning a see frame that has the position that's the same as my blue
part, but it has a rotation that is looking at my red part.
And then now I can index to get my blue part and I can override it.
See frame with this new frame I made.
Now I can run my game.
And I should be able to see this part rotate and look towards my red part.
Perfect.
I can also perform some arithmetic on the C frame.
So, for example, maybe I wanted to move my part where it's looking at this red object and then move
like five studs towards it.
To do that, I can create a new variable.
I'll call it looking see frame moved.
And I'll take my looking C frame and I'll offset it.
By the look and see frames look vector and what the look vector returns is a vector that has a distance
of one.
The line has a magnitude of one, and it's the direction of where my vector is facing.
In this case, when I rotate it, it'll be facing in some direction that's going towards my part.
And it'll move it by one stud.
If I want to move it more than one stud, then I can multiply this vector by a number and that will
be in stud.
So maybe I want to move it.
Five studs.
Then I can yield for like 3 seconds.
Otherwise, if I don't yield, this will execute immediately.
And then me setting the new frame will execute immediately.
So I won't be able to see the change.
We're just going to yield and I'll override the frame of my blue part again to this new looking C frame
moved.
Now if I run my game.
It looks at my part and then it should move five studs towards my part just like that.
I.
I can also offset my part in many other directions, such as in the Y direction.
So maybe I want to move my part upwards.
Then I can yield again.
And then I can override the C frame to the same looking C frame moved.
And instead, now I want to move it up in the Y direction by, let's say, ten studs.
So I'll create a new vector, three object new, and this will just go ten studs up in the Y direction.
Now if I run my game again.
I look at my part should move five studs towards my part.
And then move up ten studs.
Awesome.
Now I can also mess with the angles of my part by messing with the angles of the C frame.
So I'll create another variable.
I'll just call it rotated see frame and I'll set it to a new C frame object and I'll use the angles
function.
Now, in here I need to pass three numbers and these numbers have to be in radians.
And basically, if you don't know what a radiant is, it's the distance around a circle.
So it starts off at like zero.
And then halfway around the circles pi and then all the way around the circle is two pi.
And this is kind of stuff you'll learn in trigonometry if you ever did trigonometry.
And those radians have the same kind of equivalent in degrees.
So like 30 degrees around a circle has this same circumference in radians.
That's just what radians stands for.
But I don't expect you to know radians off the top of your head.
So we have this cool function called math dot rad and I'll convert degrees into radians.
So maybe I want to rotate like 180 degrees around the x axis and then maybe I want to rotate, I don't
know, 55 degrees around the Y axis.
And then maybe I want to rotate -90 degrees around the Z axis.
We'll just do something like that.
I will yield again for 3 seconds and then I will override the C frame of my blue part once more to this
new rotated C frame.
Now the problem is, is that this new rotated C frame doesn't have a position.
The default position is 000.
So this blue C frame, this blue object is going to move all the way to the origin point of my map.
And I don't want to do that.
I want to keep the same position, just rotate it randomly.
So to do that I can get my C frame and multiply it by this rotated C frame.
So basically what I'm doing is I'm adding this rotation to my C frame.
That's just the position.
And we use the multiplication operator here.
So when you want to offset C frames with another C frame, use the multiplication operator.
But if you want to offset a C frame using a vector, then you have to use the addition operator.
So let me run my game and let's see if this works.
So I look at it, I should move five studs towards it.
Move ten studs up.
And then I should rotate some random amount.
Here we go.
There's my weird rotation.
Now, the last thing I want to show you here is linear interpolation, and I explained it briefly earlier,
but like I said, it's kind of an invisible line between two points that you can place a C frame on.
So my game, I want to position this blue object between these two red objects, somewhere between these
two red objects.
So imagine an invisible line going from this object to this object, and I can put this blue object
somewhere on that line and the percentage denotes how far away from this object I want to be to this
object.
So if I do like 0.2, it'll be 20% of the way to this object.
So 0.2 would be like, right here.
And if I do 0.5, that's halfway.
So it'll be at the halfway mark.
And if I do 0.9, then it'll be almost right next to this last part.
And then one would mean it's in the exact same position as this part.
So let's show that real quick.
I'm going to index the demo folder and get my first read part and get it see frame.
And then using the C frame I can call the lookup function and pass another C frame.
The other C frame is going to be my second read object.
So basically this is my starting see frame and this is my goal see frame.
And then I can pass a floating point number to define what percentage of the way I want to make to my
second keyframe.
So for this example, I'll just do point five, which is halfway.
And then I can store the results of this in a variable.
I'll just call it to see frame.
I will yield once more for 3 seconds and then I'll override the key frame of my blue part again.
To this new looped C frame.
Let me run my game.
Okay.
We look at our object.
We move five studs to it.
We should move ten studs up.
Well, rotate randomly.
Cool.
And then we should move between these two objects.
Awesome.
Now, if you've noticed, we lost our rotation.
How do we keep our rotation?
I'll show you real quick.
So where we set our C frame?
Unfortunately, when you use this function, it returns a C frame with no rotation.
So to preserve our rotation, I'll just multiply it again by our rotated C frame.
That way we keep our rotation.
Another thing you might have noticed is actually we can clean up this line right here because we are
setting the C frame to itself.
Times this rotated C frame, we can actually just use the multiply equal operator and it achieves the
same effect.
Now, if I run my game again, we should preserve our rotation.
Perfect.
So everything worked out nicely.
So this was just a quick and brief overview of how to use see frames and roadblocks.
If you're confused on something still or you need more help, feel free to check out that robot's article
link.
Or don't be afraid to ask any questions.